package cn.edu.csust.coursecard.controller;

import cn.edu.csust.coursecard.annotation.RequiredAdmin;
import cn.edu.csust.coursecard.dao.entity.Notice;
import cn.edu.csust.coursecard.common.ReturnData;
import cn.edu.csust.coursecard.service.NoticeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("notice")
public class NoticeController {
    @Resource
    NoticeService noticeService;

    @PostMapping("/add")
    @RequiredAdmin
    public ReturnData addNotice(Notice notice) {
        return noticeService.addNotice(notice);
    }

    @GetMapping("/get")
    public ReturnData getByUserId(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        return ReturnData.success(noticeService.getByUserIdLegacy(userId));
    }

    @GetMapping("/v2/get")
    public ReturnData getNotices(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        List<Notice> byUserId = noticeService.getUser(userId);
        List<Notice> byGlobal = noticeService.getGlobal();
        byUserId.addAll(byGlobal);
        return ReturnData.success(byUserId);
    }

    @GetMapping("/getAll")
    @RequiredAdmin
    public ReturnData getAllNotice() {
        return noticeService.getAll();
    }

    @GetMapping("/delete")
    @RequiredAdmin
    public ReturnData deleteNotice(Integer id) {
        return noticeService.deleteById(id);
    }

    @GetMapping("/deleteAll")
    @RequiredAdmin
    public ReturnData deleteAllNotice() {
        return noticeService.deleteAll();
    }
}
